package com.mygdx.game.Stage.Fight;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.mygdx.game.Stage.Fight.Card.Card;

import java.util.ArrayList;

public class BasicFight extends Stage {

    //主角
    public Fighter mainActor;

    //敌人
    public ArrayList<Fighter> enemies = new ArrayList<>();

    //卡组
    public Cards cards = new Cards();

    //能量/费用
    public Energy energy = new Energy();

    //背景
    public String background;
    private Texture texture;
    private Image backgroundImage;

    public BasicFight() {
        super();
    }

    public void init() {
        Gdx.input.setInputProcessor(this);
        loadBackGround();
        loadGame();
    }

    public void loadBackGround(){
        texture = new Texture(Gdx.files.internal(background)); //"GroupProject/image/story.png"
        backgroundImage = new Image(texture);
        backgroundImage.setSize(1920, 1080);
        this.addActor(backgroundImage);
    }

    public void loadGame(){
        loadMainActor();
        loadEnemies();
        loadCards();
        loadPassButton();
        loadEnergy();
        isPassButtonPressed();
        isCardTouched();
    }

    public void loadCards(){
        for (int i=0;i<5;i++){
            cards.handCards.get(i).setCardPosition(i);
            this.addActor(cards.handCards.get(i));
        }
        this.addActor(cards.cardBack);
    }

    public void loadMainActor(){
        generateMainActor();
        this.addActor(mainActor);
    }

    public void loadEnemies(){
        generateEnemies();
        for (int i=0;i<enemies.size();i++){
            this.addActor(enemies.get(i));
        }
    }

    public void loadPassButton(){
        this.addActor(PassButton.getButton());
    }

    public void loadEnergy(){
        this.addActor(energy);
    }

    public void generateMainActor(){
    }

    public void generateEnemies(){
    }

    public boolean isCardTouched(){
        this.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                if (y>=0&&y<=400){
                    if (x>=0&&x<=300){
                        cardTouched(0);
                    }else if (x<=600){
                        cardTouched(1);
                    }else if (x<=900){
                        cardTouched(2);
                    }else if (x<=1200){
                        cardTouched(3);
                    }else if (x<=1500){
                        cardTouched(4);
                    }
                }
            }
        });
        return true;

    }

    public void cardTouched(int i){
        //触发被点卡牌对效果，并丢弃卡牌
        if (cards.handCards.get(i)==null) return;
        if (!cards.isCardsReady()) return;
        if (!cards.handCards.get(i).effect(energy,mainActor,enemies)) return;
        this.getRoot().removeActor(cards.handCards.get(i));
        cards.handCards.remove(i);

        //抽卡添加到手牌
        Card card = cards.drawCard();
        cards.handCards.add(card);
        card.setCardPosition(5);
        this.addActor(card);

        //移动手牌
        for (int j=i;j<5;j++){
            if(cards.handCards.get(j)==null) break;
            cards.handCards.get(j).startmoveNextCardPosition();
        }
    }

    public void isPassButtonPressed(){
        PassButton.getButton().addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                //每回合恢复全部能量
                energy.energy=Energy.MAX_ENERGY;

                //执行主人公回合结束动作
                mainActor.roundAction();


                //执行敌人回合结束动作
                for (Fighter fighte:enemies){
                    fighte.roundAction();
                }
            }
        });
    }
}
